﻿using System;

namespace RexToy.UI.Controls
{
    public class MessageBox : Dialog
    {
        public MessageBox(string head, TransitionMode transition, string msg, DialogButtons? buttons)
            : base(head, transition)
        {
            var content = new RexToy.UI.Controls.Content(msg, null);
            this.AppendControl(content);

            this.InitButtons(buttons ?? RexToy.UI.Controls.DialogButtons.OK);
        }

        private void InitButtons(DialogButtons buttons)
        {
            var g = new RexToy.UI.Controls.VGroupBox();
            this.AppendControl(g);

            if ((buttons & DialogButtons.OK) == DialogButtons.OK)
            {
                var btn = new RexToy.UI.Controls.Button("OK");
                btn.SetInline(true);
                btn["value"] = RexToy.UI.Controls.DialogResult.OK;
                System.Html.Console.Log(btn["init"]);
                g.AppendControl(btn);
                btn.AttachEvent("onclick", new Action<object>(this.OnClick).Bind(this));
            }
            if ((buttons & DialogButtons.Yes) == DialogButtons.Yes)
            {
                var btn = new RexToy.UI.Controls.Button("Yes");
                btn.SetInline(true);
                btn["value"] = RexToy.UI.Controls.DialogResult.Yes;
                g.AppendControl(btn);
                btn.AttachEvent("onclick", new Action<object>(this.OnClick).Bind(this));
            }
            if ((buttons & DialogButtons.No) == DialogButtons.No)
            {
                var btn = new RexToy.UI.Controls.Button("No");
                btn.SetInline(true);
                btn["value"] = RexToy.UI.Controls.DialogResult.No;
                g.AppendControl(btn);
                btn.AttachEvent("onclick", new Action<object>(this.OnClick).Bind(this));
            }
            if ((buttons & DialogButtons.Cancel) == DialogButtons.Cancel)
            {
                var btn = new RexToy.UI.Controls.Button("Cancel");
                btn.SetInline(true);
                btn["value"] = RexToy.UI.Controls.DialogResult.Cancel;
                g.AppendControl(btn);
                btn.AttachEvent("onclick", new Action<object>(this.OnClick).Bind(this));
            }
        }

        private void OnClick(object sender)
        {
            this.__dialogResult = (DialogResult)sender["value"];
            this.Close();
        }
    }
}
